<template>
  <div class="order">
    <!-- <el-form :inline="true" :model="form">
      <el-form-item label="汽车品牌" :label-width="formLabelWidth">
        <el-input v-model="listQuery.carBrand" autocomplete="off"></el-input>
      </el-form-item>
      <div></div>
          <el-button type="success" icon="el-icon-zoom-in" @click="getCarList()"
      >查询</el-button
    > -->
    <el-button type="primary" icon="el-icon-plus" @click="carAdd = true"
      >添加</el-button
    >
    <!-- </el-form> -->

    <el-dialog
      title="订单组添加"
      :visible.sync="carAdd"
      width="500px"
      :before-close="handleClose"
    >
      <el-form :model="addList" ref="cargoFormEdit">
        <el-form-item
          :required="true"
          label="选择汽车"
          :label-width="formLabelWidth"
        >
          <el-cascader
            ref="cascader"
            clearable
            placeholder="请选择汽车"
            :options="options"
            :show-all-levels="false"
            v-model="addList.purchaseOrderAddVos[0].puroGoodsId"
          ></el-cascader>
        </el-form-item>
        <!-- <el-form-item
          :required="true"
          label="选择零件"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="addList.sparePartsId"
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in stock"
              :key="item.sparePartsId"
              :label="item.sparePartsName"
              :value="item.sparePartsId"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item
          :required="true"
          label="选择仓库"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="addList.puroRepoId"
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in using"
              :key="item.repoId"
              :label="item.repoName"
              :value="item.repoId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          :required="true"
          label="选择供应商"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="addList.purchaseOrderAddVos[0].puroPurId"
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in gys"
              :key="item.id"
              :label="item.purName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="AddCarList()">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="采购订单详情"
      :visible.sync="carText"
      width="1200px"
      append-to-body
      :before-close="handleClose"
    >
      <el-table
        @current-change="handleSelectionChange"
        highlight-current-row
        :data="carTextList"
        height="60vh"
        border
        style="width: 100vw; margin: 20px auto"
      >
        <el-table-column
          prop="puroGoodsName"
          label="汽车品牌"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="puroRepoName"
          label="仓库"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="puroPurName"
          label="供应商"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="puroCount"
          label="进货量"
          width="60"
        ></el-table-column>
        <el-table-column
          prop="puroPrice"
          label="单价"
          width="160"
        ></el-table-column>
        <el-table-column
          prop="puroTotal"
          label="总价"
          width="160"
        ></el-table-column>

        <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="danger"
            @click="removeOrder(scope.$index, scope.row)"
            >删除</el-button
          >
                    <el-button
            size="mini"
            type="warning"
            @click="PutBigOrders(scope.$index, scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
      </el-table>

      <el-dialog
      title="修改采购信息"
      :visible.sync="putCar"
      width="400px"
      append-to-body
      :before-close="handleClose"
    >
      <el-form :model="putList" ref="cargoFormEdit">
        <el-form-item
          :required="true"
          label="选择汽车"
          :label-width="formLabelWidth"
        >
          <el-cascader
            ref="cascader"
            clearable
            placeholder="请选择汽车"
            :options="options"
            :show-all-levels="false"
            v-model="putList.purchaseOrderList[0].puroGoodsId"
          ></el-cascader>
        </el-form-item>
        <!-- <el-form-item
          :required="true"
          label="选择零件"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="addList.sparePartsId"
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in stock"
              :key="item.sparePartsId"
              :label="item.sparePartsName"
              :value="item.sparePartsId"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item
          :required="true"
          label="选择仓库"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="putList.puroRepoId"
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in using"
              :key="item.repoId"
              :label="item.repoName"
              :value="item.repoId"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item
          :required="true"
          label="选择供应商"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="putList.purchaseOrderList[0].puroPurId"
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in gys"
              :key="item.id"
              :label="item.purName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="putCarList()">修改</el-button>
      </span>
    </el-dialog>
    </el-dialog>

    

    <el-table
      @current-change="handleSelectionChange"
      highlight-current-row
      :data="carList"
      height="70vh"
      border
      style="width: 80%; margin: 20px auto"
    >
      <el-table-column
        prop="orderType"
        label="采购订单组名"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="orderName"
        label="采购订单"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="createdBy"
        label="创建人"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="createdTime"
        label="创建时间"
        width="200"
      ></el-table-column>

      <el-table-column fixed="right" label="操作" width="280">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="getCarCheckText(scope.$index)"
            >入库</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="removeBigOrders(scope.$index, scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="primary"
            @click="getOrder(scope.$index)"
            >详情</el-button
          >

          
        </template>
      </el-table-column>
    </el-table>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        :hide-on-single-page="true"
        @current-change="handleCurrentChange"
        :current-page="listQuery.pageNo"
        :page-sizes="[10, 50, 100]"
        :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      addList: {
        purchaseOrderAddVos: [
          {
            puroCount: "1",
            puroGoodsId: "",
            puroPrice: "",
            puroNumber: "4321",
          },
        ],
      },
      carTextList: [],
      checkCarList: {
        id: "",
      },
      listQuery: {
        pageSize: 10,
        pageNo: 1,
        orderType: "采购",
      },
      usingQuery: {
        pageSize: 100,
        pageNo: 1,
      },
      gys: [],
      total: 0,
      val: "",
      putCar: false,
      carAdd: false,
      carText: false,
      carList: [],
      putList: {
        purchaseOrderList: [
          {
            puroCount: "1",
            puroGoodsId: "",
            puroPrice: "",
            puroRepoId: "",
            puroNumber: "4321",
            puroPurId: "",
          },
        ],
      },
      stock: [],
      using: [],
      formLabelWidth: "120px",

      cargoFormAdd: {
        carName: "",
        carTypeId: "4028b29e7d4c0817017d4c0817960000",
        carBrand: "",
        createdBy: "",
        createdTime: "",
        deleted: "",
        updatedBy: "",
        updatedTime: "",
        carId: "",
        carInfoList: [
          {
            createdBy: "",
            createdTime: "",
            deleted: "",
            infoColor: "",
            infoDescription: "",
            infoEngine: "",
            infoId: "",
            infoImg: "",
            infoManufacturer: "",
            infoPrice: "",
            infoTransmission: "",
            infoVersion: "",
            parentId: "",
            updatedBy: "",
            updatedTime: "",
          },
        ],
      },
      options: [
        {
          value: "402880e47d5747e6017d5747e64e0002",
          label: "雪佛兰123",
          children: [
            {
              value: "402880e47d5747e6017d5747e64f0008",
              label: "雪佛兰豪华版",
            },
            {
              value: "402880e47d5747e6017d5747e650000e",
              label: "雪佛兰标准版",
            },
            {
              value: "402880e47d5747e6017d5747e6500010",
              label: "雪佛兰舒适版",
            },
          ],
        },
        {
          value: "402880e47d5747e6017d5747e64f0003",
          label: "保时捷",
          children: [
            {
              value: "402880e47d5747e6017d5747e64f0007",
              label: "保时捷舒适版",
            },
            {
              value: "402880e47d5747e6017d5747e650000a",
              label: "保时捷豪华版",
            },
          ],
        },
        {
          value: "402880e47d5747e6017d5747e64f0004",
          label: "别克",
          children: [
            {
              value: "402880e47d5747e6017d5747e64f0009",
              label: "别克标准版",
            },
            {
              value: "402880e47d5747e6017d5747e650000c",
              label: "别克舒适版",
            },
            {
              value: "402880e47d5747e6017d5747e650000f",
              label: "别克豪华版",
            },
          ],
        },
        {
          value: "402880e47d5747e6017d5747e7af0014",
          label: "比亚迪",
          children: [
            {
              value: "402880e47d5747e6017d5747e7af0015",
              label: "比亚迪标准版",
            },
          ],
        },
        {
          value: "402880e47d5747e6017d5747e7f3001b",
          label: "海马",
          children: [
            {
              value: "402880e47d5747e6017d5747e7f3001c",
              label: "海马标准版",
            },
          ],
        },
        {
          value: "402880e47d5747e6017d5747e831001e",
          label: "江淮",
          children: [
            {
              value: "402880e47d5747e6017d5747e831001f",
              label: "江淮舒适版",
            },
          ],
        },
        {
          value: "402880e47d5747e6017d5747e8320020",
          label: "奇瑞",
          children: [
            {
              value: "402880e47d5747e6017d5747e8330021",
              label: "奇瑞标准版",
            },
            {
              value: "402880e47d5747e6017d5747e8330022",
              label: "奇瑞舒适版",
            },
            {
              value: "402880e47d5747e6017d5747e8330023",
              label: "奇瑞豪华版",
            },
          ],
        },
        {
          value: "402880e47d5747e6017d5747e8370027",
          label: "宝马",
          children: [
            {
              value: "shejiyuanze",
              label: "宝马豪华版",
            },
          ],
        },
        {
          value: "402880e47d5747e6017d5747e8b2002c",
          label: "红旗",
          children: [
            {
              value: "402880e47d5747e6017d5747e8b2002d",
              label: "红旗标准版",
            },
            {
              value: "402880e47d5747e6017d5747e8b2002e",
              label: "红旗舒适版",
            },
          ],
        },
        {
          value: "402880e47d5747e6017d5747e8ef0034",
          label: "三菱",
          children: [
            {
              value: "402880e47d5747e6017d5747e8ef0035",
              label: "三菱舒适版",
            },
          ],
        },
      ],
    };
  },
  created() {
    this.getCarList();
    this.getUsingList();
    this.getStockList();
    this.getgysList();
  },
  methods: {
    getgysList() {
      let token = window.localStorage.getItem("token");
      this.$axios({
        method: "get",
        url: this.GLOBAL.gys,
        params: this.usingQuery,
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          this.gys = result.data.result.records;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getCarList() {
      let token = window.localStorage.getItem("token");
      this.$axios({
        method: "get",
        url: this.GLOBAL.checkPurchase,
        params: this.listQuery,
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          this.carList = result.data.result.records;
          this.total = result.data.result.total;
        })
        .catch((err) => {
          console.log(err);
        });
    },

    PutBigOrders(index) {
      // demo
      this.putList = {
        purchaseOrderList:this.carTextList[index]
      }
      
      
      console.log(this.putList);
      this.putCar = true
      console.log(1);
      console.log(this.putList);
    },
    removeBigOrders(index, row) {
      console.log(index, row);
      let ids = this.carList[index].id;
      let token = window.localStorage.getItem("token");
      this.$axios({
        method: "delete",
        url: this.GLOBAL.delectPurchase,
        params: { id: ids },
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          console.log(result);
          if (result.data.code == 200 || result.data.code == "200") {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getCarList();
          } else {
            this.$message({
              type: "warning",
              message: "删除失败!",
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    handleSelectionChange(val) {
      this.checkCarList.id = val.id;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => {});
    },
    putCarList() {
      let token = window.localStorage.getItem("token");
      this.$axios({
        method: "post",
        url: this.GLOBAL.addPurchase,
        data: this.putList,
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          if (result.data.code == 200 || result.data.code == "200") {
            this.$message({
              type: "success",
              message: result.data.message,
            });
            this.putCar = false;
            this.getCarList();
          } else {
            this.$message({
              type: "warning",
              message: result.data.message,
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    AddCarList() {
      let token = window.localStorage.getItem("token");
      let add = this.addList.purchaseOrderAddVos[0];
      this.addList.purchaseOrderAddVos[0].puroGoodsId = add.puroGoodsId[1];
      this.$axios({
        method: "post",
        url: this.GLOBAL.addPurchase,
        data: this.addList,
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          if (result.data.code == 200 || result.data.code == "200") {
            this.$message({
              type: "success",
              message: result.data.message,
            });
            this.carAdd = false;
            this.getCarList();
          } else {
            this.$message({
              type: "warning",
              message: result.data.message,
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val;
      this.getCarList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNo = val;
      this.getCarList();
    },

    getCarCheckText(index) {
      let token = window.localStorage.getItem("token");
      let ids = this.carList[index].id;
      this.$axios({
        method: "get",
        url: this.GLOBAL.ruku,
        params: { id: ids },
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          console.log(result);
          this.$message({
            type: "success",
            message: "入库成功",
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },

    getUsingList() {
      let token = window.localStorage.getItem("token");
      this.$axios({
        method: "get",
        url: this.GLOBAL.checkUsing,
        params: this.usingQuery,
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          this.stock = result.data.result.records;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getStockList() {
      let token = window.localStorage.getItem("token");
      this.$axios({
        method: "get",
        url: this.GLOBAL.checkStock,
        params: this.usingQuery,
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          this.using = result.data.result.records;
        })
        .catch((err) => {
          console.log(err);
        });
    },

    getOrder(index) {
      let token = window.localStorage.getItem("token");
      console.log(index);
      this.$axios({
        method: "get",
        url: this.GLOBAL.purId,
        params: {
          id:this.carList[index].id
        },
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          this.carTextList = result.data.result;
          this.carText = true;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    removeOrder(index,row){
      let token = window.localStorage.getItem("token");
      let ids = row.id
      console.log(row);
      this.$axios({
        method: "delete",
        url: this.GLOBAL.delPur,
        params: { id: ids },
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          if (result.data.code == 200 || result.data.code == "200") {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //this.getCarList();
            this.getCarList()
            this.carText = false;
            
          } else {
            this.$message({
              type: "warning",
              message: "删除失败!",
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    }
  },
};
</script>

<style>
</style>